/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/15 5:46 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import { getChildTreeData } from 'utils/index';
import {
    changeDepartmentInfo,
    createFirstLevelDepartment,
    createLowerDepartment,
    getDepartmentTreeList,
} from 'api/index';

export default {
    namespace: 'units',
    state: {
        treeData: [],
        modalVisible: false,
        superiorDepartmentId: 0,
    },
    reducers: {
        setUnitsTreeData(state, { payload: treeData }) {
            return { ...state, treeData };
        },
        setModalVisible(state, { payload: modalVisible }) {
            return { ...state, modalVisible };
        },
        setSuperiorDepartmentId(state, { payload: superiorDepartmentId }) {
            return { ...state, superiorDepartmentId };
        },
    },
    effects: {
        /**
         * @desc 获取树
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getUnitsTreeData({ payload }, { call, put, select }) {
            const result = yield call(getDepartmentTreeList, payload);
            if (result.status === 'success') {
                const data = result.result;
                const treeData = data.map(c => {
                    if (!c.hasLowerDepartment) {
                        return {
                            title: c.departmentName,
                            key: c.departmentId,
                            departmentType:c.departmentType,
                            comment: c.comment,
                            hasLowerDepartment: c.hasLowerDepartment,
                            level: c.level,
                        };
                    } else {
                        return {
                            title: c.departmentName,
                            key: c.departmentId,
                            departmentType:c.departmentType,
                            comment: c.comment,
                            hasLowerDepartment: c.hasLowerDepartment,
                            level: c.level,
                            children: getChildTreeData(c.lowerDepartmentModel),
                        };
                    }
                });
                yield put({
                    type: 'setUnitsTreeData',
                    payload: treeData,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 创建一级单位
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addFirstLevelDepartment({ payload, callback }, { call, put, select }) {
            const result = yield call(createFirstLevelDepartment, payload);
            if (result.status === 'success') {
                message.success(`添加${payload.departmentName}成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        /**
         * @desc 创建下级单位
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addLowerDepartment({ payload, callback }, { call, put, select }) {
            const result = yield call(createLowerDepartment, payload);
            if (result.status === 'success') {
                message.success(`添加${payload.departmentName}成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 修改单位
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* updateDepartmentInfo({ payload, callback }, { call, put, select }) {
            const result = yield call(changeDepartmentInfo, payload);
            if (result.status === 'success') {
                message.success(`修改${payload.departmentName}成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp('/units').exec(pathname)) {
                    dispatch({
                        type: 'getUnitsTreeData',
                        payload: {},
                    });
                }
            });
        },
    },
};
